/**
 *
 */
package forum;

import java.util.ArrayList;
import java.util.List;

import utils.LogHelper;

/**
 * @author Olga
 * 
 */
public class ForumsHandler {

	private List<Forum> forums;
	private int forumIdCounter;
	private int postIdCounter;

	public ForumsHandler() {
		this.forums = new ArrayList<Forum>();
		this.forumIdCounter = 0;
		this.postIdCounter = 0;
	}

	/**
	 * 
	 * @param subject
	 * @return true on success. False when the forum with this subject is
	 *         already exists.
	 */
	public synchronized int addForum(String subject) {
		if (!this.isExist(subject)) {
			Forum f = new Forum(this.forumIdCounter, subject);
			this.forumIdCounter++;
			LogHelper.instance().log("Adding forum with id: " + f.getID());
			if(this.forums.add(f)) {
				return f.getID();
			}
		}
		LogHelper.instance().log(
				"Forum with subject " + subject + " is already exist");
		return -1;
	}

	/**
	 * 
	 * @param subject
	 * @return true if forum with subject exist, false otherwise
	 */
	private synchronized boolean isExist(String subject) {
		for (Forum f : this.forums)
			if (f.getSubject().equalsIgnoreCase(subject))
				return true;
		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @param subject
	 *            of thread
	 * @param content
	 *            of thread
	 * @param author
	 *            of threads
	 * @return true on success, false else.
	 */
	public synchronized int addThread(int forumID, String subject,
		String content, String author) {
		Forum forum = this.getForum(forumID);
		if (forum != null) {
			Thread toAdd = new Thread(this.postIdCounter, subject, content,
					author);
			this.postIdCounter++;
			if (forum.addThread(toAdd)) {
				return toAdd.getID();
			}
		}
		return -1;
	}

	/**
	 * 
	 * @param forumID
	 * @param postID
	 *            - post ID to add reply to it
	 * @param subject
	 * @param content
	 * @param author
	 * @return true on success, false else
	 */
	public synchronized int addReply(int forumID, int postID,
			String subject, String content, String author) {
		Forum forum = this.getForum(forumID);
		if (forum != null) {
			int id = forum.addReply(postID, subject, content, author, postIdCounter);
			if (id != -1) {
				this.postIdCounter++;
				return id;
			} else
				return -1;

		} else {
			LogHelper.instance().log(
					"Forum with id " + forumID + " was not found");
			return -1;
		}
	}

	/**
	 * @param username
	 *            of user who wants to edit the post
	 * @param forumID
	 * @param postID
	 *            - to edit
	 * @param content
	 * @return true on success. false if forum with forumId was not found
	 */
	public synchronized boolean editPost(String username, int forumID,
			int postID, String content) {
		Forum forum = this.getForum(forumID);
		if (forum != null) {
			return forum.editPost(username, postID, content);
		}
		LogHelper.instance().log(
				"Cannot edit post " + postID + ". Forum " + forumID
						+ " not found");
		return false;
	}

	/**
	 * @param forumID
	 * @param postID
	 * @return true on success, false else
	 */
	public synchronized boolean removePost(int forumID, int postID) {
		for (Forum f : this.forums)
			if (f.getID() == forumID)
				return f.removePost(postID);
		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @param rootPostID
	 * @return
	 */
	public synchronized boolean removeThread(int forumID, int rootPostID) {
		for (Forum f : this.forums)
			if (f.getID() == forumID)
				return f.removeThread(rootPostID);
		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @return
	 */
	public synchronized boolean removeForum(int forumID) {
		for (int i = 0; i < this.forums.size(); i++)
			if (this.forums.get(i).getID() == forumID) {
				LogHelper.instance().log("Removing forum with id: " + forumID);
				return (this.forums.remove(i) != null);
			}
		LogHelper.instance().log(
				"Cannnot remove forum " + forumID + ". It was not found.");
		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @return forum with ID forumID if exist, else return null
	 */
	public synchronized Forum getForum(int forumID) {
		for (Forum f : this.forums)
			if (f.getID() == forumID) {
				LogHelper.instance().log("Getting forum with id: " + forumID);
				return f;
			}
		LogHelper
				.instance()
				.log("Forum "
						+ forumID
						+ " not found in getForum method in ForumsHandler class.");
		return null;
	}

	/**
	 * 
	 * @param rootPostID
	 * @return reference to thread with rootID = rootPostID. If such thread not
	 *         found, return null
	 */
	public synchronized Thread getTread(int rootPostID) {
		Thread ans = null;
		for (Forum f : this.forums)
			if ((ans = f.getThread(rootPostID)) != null)
				break;
		return ans;
	}

	public synchronized Post getPost(int postID) {
		Post ans = null;
		for (Forum f : this.forums)
			if ((ans = f.findPost(postID)) != null)
				break;
		return ans;
	}

	/**
	 * 
	 * @return only topics of forums
	 */
	public synchronized String showForums() {
		String ans = "";
		for (Forum f : this.forums) {
			ans += f.showForumTopics();
		}
		return ans + "\0";
	}

	/**
	 * 
	 * @return all forums trees
	 */
	public synchronized String showAllForums() {
		String ans = "";
		for (Forum f : this.forums)
			ans += f.showAllForum();
		return ans;
	}

	/**
	 * @return the forums
	 */
	public synchronized List<Forum> getForums() {
		return forums;
	}

}
